package com.cl.edu.service.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.common.util.StringUtil;
import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.BookingteacherDAO;
import com.cl.edu.dao.StudentDAO;
import com.cl.edu.dao.TeacherDAO;
import com.cl.edu.entity.Bookingteacher;
import com.cl.edu.entity.Student;
import com.cl.edu.entity.Teacher;

/**
 * 学生主动预约教员信息管理
 * @author Administrator
 *
 */
@Service
@Transactional
public class BookingteacherManager {
	@Autowired
	private BookingteacherDAO bookingteacherDAO;
	@Autowired
	private TeacherDAO teacherDAO;
	@Autowired
	private StudentDAO studentDAO;
	
	/**
	 * 根据id得到预约信息
	 * @param id
	 * @return
	 */
	public Bookingteacher getBookingteacher(Long id) {
		return bookingteacherDAO.get(id);
	}
	
	/**
	 * 删除预约
	 * @param id
	 */
	public void delBookingteacher(Long id) {
		bookingteacherDAO.delete(id);
	}
	
	/**
	 * 更新预约信息
	 * @param entity
	 */
	public void updateBooking(Bookingteacher entity) {
		bookingteacherDAO.saveOrUpdate(entity);
	}
	
	/**
	 * 得到教员
	 * @param id
	 * @return
	 */
	public Teacher getTeacher(Long id) {
		return teacherDAO.get(id);
	}
	
	/**
	 * 得到学员
	 * @param id
	 * @return
	 */
	public Student getStudent(Long id) {
		return studentDAO.get(id);
	}
	
	/**
	 * 分页查询预约信息
	 * @param page
	 * @return
	 */
	public Page<Bookingteacher> getBookingteachers(Page<Bookingteacher> page) {
		StringBuffer hql = new StringBuffer("from Bookingteacher model ");
		hql.append(" where 1=1 ");
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("realname".equals(filter.getPropertyName())) {
				hql.append(" and model.teacher.realname like '%")
						.append(filter.getPropertyValue()).append("%'");
			}else if ("name".equals(filter.getPropertyName())) {
				hql.append(" and model.student.name like '%")
				.append(filter.getPropertyValue()).append("%'");
			}else if ("state".equals(filter.getPropertyName())&&StringUtil.isNotEmpty(filter.getPropertyValue())) {
				hql.append(" and model.state = ")
				.append(filter.getPropertyValue());
			}
		}
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = bookingteacherDAO.findPage(page, hql.toString());
		return page;
	}
}
